package com.ruoyi.make.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.make.service.CpProductService;
import com.ruoyi.system.api.CodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.make.domain.ScgdProductionWorkOrder;
import com.ruoyi.make.service.IScgdProductionWorkOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生产工单Controller
 * 
 * @author ruoyi
 * @date 2024-06-26
 */
@RestController
@RequestMapping("/order")
public class ScgdProductionWorkOrderController extends BaseController
{
    @Autowired
    private IScgdProductionWorkOrderService scgdProductionWorkOrderService;

    @Autowired
    private CpProductService cpProductService;

    /**
     * 查询生产工单列表
     */
    @RequiresPermissions("system:order:list")
    @GetMapping("/list")
    public TableDataInfo list(ScgdProductionWorkOrder scgdProductionWorkOrder)
    {
        startPage();
        List<ScgdProductionWorkOrder> list = scgdProductionWorkOrderService.findAll(scgdProductionWorkOrder);
        System.out.println("------------------:"+ JSON.toJSONString(scgdProductionWorkOrderService.findAll(scgdProductionWorkOrder)));
        return getDataTable(list);
    }

    /**
     * 导出生产工单列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "生产工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScgdProductionWorkOrder scgdProductionWorkOrder)
    {
        List<ScgdProductionWorkOrder> list = scgdProductionWorkOrderService.findAll(scgdProductionWorkOrder);
        ExcelUtil<ScgdProductionWorkOrder> util = new ExcelUtil<ScgdProductionWorkOrder>(ScgdProductionWorkOrder.class);
        util.exportExcel(response, list, "生产工单数据");
    }

    /**
     * 获取生产工单详细信息
     */
    @RequiresPermissions("system:order:query")
    @GetMapping(value = "/{gdCode}")
    public AjaxResult getInfo(@PathVariable("gdCode") String gdCode)
    {
        return success(scgdProductionWorkOrderService.selectScgdProductionWorkOrderByGdCode(gdCode));
    }

    /**
     * 获取生产工单关联Bom
     */
    @RequiresPermissions("system:order:Bom")
    @GetMapping(value = "/Bom")
    public AjaxResult getBom(String gdCode)
    {
        System.out.println("============================"+gdCode);
        return success(scgdProductionWorkOrderService.selectGdAndBom(gdCode));
    }

    /**
     *    获取产品关联属性
     * */
    @RequiresPermissions("system:order:value")
    @GetMapping(value = "/value")
    public AjaxResult getAttribute(String cpCode){
        System.out.println("================="+cpCode);
        return success(cpProductService.selectProductAttributes(cpCode));
    }

    /**
     *   修改工单状态
     * */
    @RequiresPermissions("system:order:status")
    @GetMapping(value="/gdStatus")
    public AjaxResult updateGdStatus(@RequestParam("gdCode") String gdCode, @RequestParam("status") String status){
        System.out.println("gdCode: " + gdCode + ", status: " + status);
        return toAjax(scgdProductionWorkOrderService.updateGdStatus(gdCode,status));
    }


    /**
     * 新增生产工单
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ScgdProductionWorkOrder scgdProductionWorkOrder)
    {

        return toAjax(scgdProductionWorkOrderService.insertScgdProductionWorkOrder(scgdProductionWorkOrder));
    }

    /**
     * 修改生产工单
     */
    @RequiresPermissions("system:order:put")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody ScgdProductionWorkOrder scgdProductionWorkOrder)
    {
        return toAjax(scgdProductionWorkOrderService.updateScgdProductionWorkOrder(scgdProductionWorkOrder));
    }

    /**
     * 删除生产工单
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "生产工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gdCodes}")
    public AjaxResult remove(@PathVariable String[] gdCodes)
    {
        return toAjax(scgdProductionWorkOrderService.deleteScgdProductionWorkOrderByGdCodes(gdCodes));
    }
    @Resource
    private CodeClient codeClient;
    @GetMapping("/getCode")
    public String getCode(Integer id){
        return codeClient.getCode(id);
    }
}
